<?php

//
// +---------------------------------------------------------+
// | 开发组
// +---------------------------------------------------------+
// | Copyrigh  (c)  2020 The PHP Group
// +---------------------------------------------------------+
// | 商户 代理商
// +---------------------------------------------------------+
// | zxc
// | php后端开发
// |
// +———————————————————+
//

namespace App\Models\Admin;

use App\Tools\Financial;
use App\Tools\Prstatic;
use Illuminate\Database\Eloquent\Model;

class StoreGoods extends Model
{
    protected $table = 'store_goods';
    protected $primaryKey = 'id';
    protected $dateFormat = 'U'; // 时间戳

    /**
     * 添加库存
     */
    public static function upGoodsNumByOid($store_id, $goods_id, $goods_num, $create_user, $order_id = 0, $money = 0)
    {
        if ($store_id && $goods_id && $goods_num) {
            $info = self::where(['store_id' => $store_id, 'goods_id' => $goods_id])->first();
            if ($info) {
                // 商户加上库存
                self::where(['store_id' => $store_id, 'goods_id' => $goods_id])->increment('goods_total_num', $goods_num);
                self::where(['store_id' => $store_id, 'goods_id' => $goods_id])->increment('goods_sale_num', $goods_num);
            } else {
                $data['store_id'] = $store_id;
                $data['goods_id'] = $goods_id;
                $data['goods_total_num'] = $goods_num;
                $data['goods_sale_num'] = $goods_num;
                $data['goods_sale_num'] = $goods_num;
                $data['goods_sale_num'] = $goods_num;
                $data['created_at'] = time();
                $data['updated_at'] = time();
                $data['create_user'] = $create_user;
                $data['update_user'] = $create_user;
                $data['remark'] = '补货';
                self::insert($data);
            }
            StoreGoodsRepertoryLog::addInfoByGoodsId($order_id, $goods_id, $goods_num, $store_id, 2, $create_user, $money);
        }
        return true;
    }
}